package com.suray.system.run.seer.http;

import com.alibaba.fastjson.JSON;
import com.suray.basic.wcs.utils.LoggerUtil;
import com.suray.commin.core.utils.SpringContextHolder;
import com.suray.system.run.seer.http.bean.*;
import com.suray.wcs.service.config.properties.WcsProperties;
import com.suray.wcs.service.core.util.HttpClientSpring;
import com.suray.wcs.service.enums.NetAddressType;
import com.suray.wcs.service.system.service.impl.NetAddressManageDBServiceImpl;
import com.suray.wcs.service.util.WcsUtil;
import com.suray.wcs.service.wmsconnect.ToHkAgv;
import org.apache.commons.lang3.StringUtils;

public class ToSeerAgv {

    private static NetAddressManageDBServiceImpl netAddressManageDBService =
            SpringContextHolder.getBean(NetAddressManageDBServiceImpl.class);

    final public static String createTask = "/setOrder";
    final public static String inquireTask = "/orderDetails";
    final public static String addBlockPath = "/addBlocks";
    final public static String markCompletePath = "/markComplete ";

    private static String getSeerAgvPort() {
        return netAddressManageDBService.findNetAddressByType(NetAddressType.SEER_AGV).get(0).getPort();
    }

    private static String getSeerAgvIp() {
        return netAddressManageDBService.findNetAddressByType(NetAddressType.SEER_AGV).get(0).getIp();
    }

    /**
     * 仙工Agv任务发送
     * @param task
     * @return
     */
    public static boolean seerSendTask(SeerAgvTaskSend task) {
        try {
            LoggerUtil.info("仙工Agv任务发送=>" + JSON.toJSONString(task), ToSeerAgv.class);
            String result = StringUtils.EMPTY;
            for (int i = 0; i < WcsProperties.ATTEMPT_NUM_3; i++) {
                try {
                    result = HttpClientSpring.sendPostRequest(
                            WcsUtil.getHttpUrl(getSeerAgvIp(), getSeerAgvPort(), createTask),
                            JSON.toJSONString(task));
                    LoggerUtil.info("仙工Agv任务发送 成功:" + result, ToSeerAgv.class);
                    return true;
                } catch (Exception e) {
                    LoggerUtil.info("仙工Agv任务发送 接口异常：" + e.getMessage(), ToSeerAgv.class);
                    WcsUtil.exceptionLog(e, ToSeerAgv.class);
                    WcsUtil.delay(1000);
                }
            }

            LoggerUtil.info("仙工Agv任务发送 失败", ToSeerAgv.class);
            return false;
        } catch (Exception e) {
            LoggerUtil.error("仙工Agv任务发送 接口解析返回值出现异常！" + e.getMessage(), ToSeerAgv.class);
            WcsUtil.exceptionLog(e, ToHkAgv.class);
            return false;
        }
    }

    /**
     * 仙工Agv任务查询
     * @param taskId
     * @return
     */
    public static SeerAgvTaskInquire inquireTask(String taskId) {
        try {
            LoggerUtil.info("仙工Agv任务查询=>" + JSON.toJSONString(taskId), ToSeerAgv.class);
            String result = StringUtils.EMPTY;
            for (int i = 0; i < WcsProperties.ATTEMPT_NUM_3; i++) {
                try {
                    result = HttpClientSpring.sendGetRequest(
                            WcsUtil.getHttpUrl(getSeerAgvIp(), getSeerAgvPort(), inquireTask) + "/" + taskId,
                            null);
                    if (StringUtils.isNotBlank(result)) {
                        break;
                    }
                } catch (Exception e) {
                    LoggerUtil.info("仙工Agv任务查询 接口异常：" + e.getMessage(), ToSeerAgv.class);
                    WcsUtil.exceptionLog(e, ToSeerAgv.class);
                    WcsUtil.delay(1000);
                }
            }
            LoggerUtil.info("仙工Agv任务查询 return data:" + result, ToSeerAgv.class);
            if (StringUtils.isBlank(result)) {
                return null;
            }

            SeerAgvTaskInquire seerAgvTaskInquire = JSON.parseObject(result, SeerAgvTaskInquire.class);

            if (seerAgvTaskInquire.getId().equals(taskId)) {
                return seerAgvTaskInquire;
            }

            LoggerUtil.info("仙工Agv任务查询 返回数据的任务id不匹配-->任务Id:" + taskId + ":" + seerAgvTaskInquire,
                    ToSeerAgv.class);
            return null;
        } catch (Exception e) {
            LoggerUtil.error("仙工Agv任务查询 接口解析返回值出现异常！" + e.getMessage(), ToSeerAgv.class);
            WcsUtil.exceptionLog(e, ToHkAgv.class);
            return null;
        }
    }

    /**
     * 添加任务
     * @param addBlock
     * @return
     */
    public static boolean addBlock(SeerAgvAddBlock addBlock) {
        try {
            LoggerUtil.info("添加任务=>" + JSON.toJSONString(addBlock), ToSeerAgv.class);
            String result = StringUtils.EMPTY;
            for (int i = 0; i < WcsProperties.ATTEMPT_NUM_3; i++) {
                try {
                    result = HttpClientSpring.sendPostRequest(
                            WcsUtil.getHttpUrl(getSeerAgvIp(), getSeerAgvPort(), addBlockPath),
                            JSON.toJSONString(addBlock));
                    LoggerUtil.info("添加任务 通讯成功:" + result, ToSeerAgv.class);
                    return true;
                } catch (Exception e) {
                    LoggerUtil.info("添加任务 接口异常：" + e.getMessage(), ToSeerAgv.class);
                    WcsUtil.exceptionLog(e, ToSeerAgv.class);
                    WcsUtil.delay(1000);
                }
            }
            LoggerUtil.info("添加任务 通讯失败", ToSeerAgv.class);
            return false;
        } catch (Exception e) {
            LoggerUtil.error("添加任务 接口解析返回值出现异常！" + e.getMessage(), ToSeerAgv.class);
            WcsUtil.exceptionLog(e, ToHkAgv.class);
            return false;
        }
    }

    /**
     * 任务封口
     * @param markComplete
     * @return
     */
    public static boolean markComplete(SeerAgvMarkComplete markComplete) {
        try {
            LoggerUtil.info("任务封口=>" + JSON.toJSONString(markComplete), ToSeerAgv.class);
            String result = StringUtils.EMPTY;
            for (int i = 0; i < WcsProperties.ATTEMPT_NUM_3; i++) {
                try {
                    result = HttpClientSpring.sendPostRequest(
                            WcsUtil.getHttpUrl(getSeerAgvIp(), getSeerAgvPort(), markCompletePath),
                            JSON.toJSONString(markComplete));
                    LoggerUtil.info("任务封口 通讯成功:" + result, ToSeerAgv.class);
                    return true;
                } catch (Exception e) {
                    LoggerUtil.info("任务封口 接口异常：" + e.getMessage(), ToSeerAgv.class);
                    WcsUtil.exceptionLog(e, ToSeerAgv.class);
                    WcsUtil.delay(1000);
                }
            }
            LoggerUtil.info("任务封口 通讯失败", ToSeerAgv.class);
            return false;
        } catch (Exception e) {
            LoggerUtil.error("任务封口 接口解析返回值出现异常！" + e.getMessage(), ToSeerAgv.class);
            WcsUtil.exceptionLog(e, ToHkAgv.class);
            return false;
        }
    }
}
